import { createRouter, createWebHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { setupRouterGuard } from './guard'

// 静态路由
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: '/redirect',
    component: () => import('@/layout/index.vue'),
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue'),
      },
    ],
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录',
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('@/views/error/404.vue'),
    meta: {
      title: '页面不存在',
      hidden: true,
    },
  },
  {
    path: '/401',
    name: '401',
    component: () => import('@/views/error/401.vue'),
    meta: {
      title: '权限不足',
      hidden: true,
    },
  },
  {
    path: '/',
    component: () => import('@/layout/index.vue'),
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index.vue'),
        meta: {
          title: '首页',
          icon: 'HomeFilled',
          affix: true,
        },
      },
    ],
  },
  {
    path: '/personal',
    component: () => import('@/layout/index.vue'),
    children: [
      {
        path: 'profile',
        name: 'Profile',
        component: () => import('@/views/personal/profile.vue'),
        meta: {
          title: '个人中心',
          icon: 'User',
          hidden: true,
        },
      },
    ],
  },
]

// 动态路由（权限路由）
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: '/system',
    component: () => import('@/layout/index.vue'),
    redirect: '/system/user',
    meta: {
      title: '系统管理',
      icon: 'Setting',
      perms: ['system:user:list', 'system:role:list', 'system:menu:list', 'system:dept:list'],
    },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/system/user/index.vue'),
        meta: {
          title: '用户管理',
          icon: 'User',
          perms: ['system:user:list'],
        },
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/system/role/index.vue'),
        meta: {
          title: '角色管理',
          icon: 'UserFilled',
          perms: ['system:role:list'],
        },
      },
      {
        path: 'menu',
        name: 'Menu',
        component: () => import('@/views/system/menu/index.vue'),
        meta: {
          title: '菜单管理',
          icon: 'Menu',
          perms: ['system:menu:list'],
        },
      },
      {
        path: 'dept',
        name: 'Dept',
        component: () => import('@/views/system/dept/index.vue'),
        meta: {
          title: '部门管理',
          icon: 'OfficeBuilding',
          perms: ['system:dept:list'],
        },
      },
      {
        path: 'dict',
        name: 'Dict',
        component: () => import('@/views/system/dict/index.vue'),
        meta: {
          title: '字典管理',
          icon: 'Collection',
          perms: ['system:dict:list'],
        },
      },
      {
        path: 'config',
        name: 'Config',
        component: () => import('@/views/system/config/index.vue'),
        meta: {
          title: '参数设置',
          icon: 'Tools',
          perms: ['system:config:list'],
        },
      },
    ],
  },
  {
    path: '/monitor',
    component: () => import('@/layout/index.vue'),
    redirect: '/monitor/operlog',
    meta: {
      title: '系统监控',
      icon: 'Monitor',
      perms: ['monitor:operlog:list', 'monitor:logininfor:list'],
    },
    children: [
      {
        path: 'operlog',
        name: 'Operlog',
        component: () => import('@/views/monitor/operlog/index.vue'),
        meta: {
          title: '操作日志',
          icon: 'Document',
          perms: ['monitor:operlog:list'],
        },
      },
      {
        path: 'logininfor',
        name: 'Logininfor',
        component: () => import('@/views/monitor/logininfor/index.vue'),
        meta: {
          title: '登录日志',
          icon: 'Key',
          perms: ['monitor:logininfor:list'],
        },
      },
    ],
  },
]

// 404路由
export const notFoundRoute: RouteRecordRaw = {
  path: '/:pathMatch(.*)*',
  redirect: '/404',
  meta: {
    hidden: true,
  },
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [...constantRoutes, notFoundRoute],
  scrollBehavior: () => ({ left: 0, top: 0 }),
})

// 设置路由守卫
setupRouterGuard(router)

export default router
